package com.sapethemape.graphics
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import mx.core.UIComponent;

	public class RemoteImage extends UIComponent
	{
		private var ldr:Loader;
		private var _url:String;
		
		private var fillbg:Boolean = false;
		
		public function set fillBG(fbg:Boolean):void{
			fillbg = fbg;
		}
		
		public function RemoteImage(url:String = '')
		{
			super();
			//var url:String = "http://hq.shockfish.com:8080/"+paxid+".jpg";//"file:///C:/wall/"+paxid+".jpg";
            //trace(url);
            if(url)
            {
            	this.url = url;
            }
		}
		
		public function get url():String{
			return _url;
		}
		public function set url(url:String):void{
			_url = url;
			drawImage();
		}
		private function drawImage():void {
			if(ldr)
			{
				removeChild(ldr);
			}
			ldr = new Loader();
            var urlReq:URLRequest = new URLRequest(_url);
            ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            ldr.load(urlReq);
            addChild(ldr);
        }
 		private function completeHandler(event:Event):void{
 			//trace('image loaded coplete');
 			//trace(ldr.x,ldr.width,ldr.y,ldr.height);
            if(fillbg)
			{
				ldr.width = width;
				ldr.height = height;
			}else
			{
				ldr.x = ldr.width / -2;
            	ldr.y = ldr.height / -2;
			}
            //trace(ldr.x,ldr.width,ldr.y,ldr.height);
 		}
        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Unable to load image: " );
        }
	}
}